package com.onlinetp.test;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.onlinetp.util.RSAUtil;
import javafx.scene.transform.Scale;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws UnsupportedEncodingException {
//        KeyPair pair = SecureUtil.generateKeyPair("RSA");
//        PrivateKey privateKey = pair.getPrivate();
//        PublicKey publicKey = pair.getPublic();
//
//        String prk = new String(Base64Utils.encode(privateKey.getEncoded()));
//        String pbk = new String(Base64Utils.encode(publicKey.getEncoded()));
//        System.out.println(prk);
//        System.out.println(pbk);
//
//        RSA rsa1 = new RSA(null,publicKey);
//        RSA rsa2 = new RSA(privateKey,null);
//        byte[] encrypt = rsa2.encrypt("asd".getBytes(), KeyType.PrivateKey);
//
//        byte[] bytes = rsa1.decrypt(encrypt, KeyType.PublicKey);
//        System.out.println(new String(bytes));

        //获得私钥
//        String privateKeyBase64 = rsa.getPrivateKeyBase64();
//        System.out.println(privateKeyBase64);
        //获得公钥
//        String publicKeyBase64 = rsa.getPublicKeyBase64();
//        System.out.println(publicKeyBase64);

//        byte[] encrypt = rsa.encrypt("asd".getBytes(), KeyType.PublicKey);

//        Scanner s = new Scanner(System.in);
//        while(true){
//
//            RSA rsa = new RSA(privateKey,null);
//            String txt = s.next();
//            System.out.println(txt);
//            byte[] decrypt = rsa.decrypt(Base64Utils.decode(txt.getBytes("UTF-8")), KeyType.PrivateKey);
//            System.out.println(new String(decrypt));
//        }

//        encByPbkey();
        t1();

    }

    // 后端解密前端密文
    public static void t1(){
        Map<String, Key> rsaKey = RSAUtil.getRSAKey();
        PublicKey publicKey = (PublicKey) rsaKey.get(RSAUtil.PB_KEY);
        PrivateKey privateKey = (PrivateKey) rsaKey.get(RSAUtil.PR_KEY);

        System.out.println(new String(Base64Utils.encode(publicKey.getEncoded())));

        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();

        String s1 = RSAUtil.dencByPrKey((PrivateKey) rsaKey.get(RSAUtil.PR_KEY), s);
        System.out.println(s1);

    }


    public static void t() throws UnsupportedEncodingException {
        String pbk_c = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk5VYxgfvdGRjdze7N8kSvW9qt4fqgzKT8yqeEKL/78KhazL+tzdnZQW+Evqi8BOdfbr+4f0ecLOspB+GSmYqRd74eOiqv4cfL6UnCU1dcEf8t9iaa57ljumTKmKs0YxYsptZcwtp0iAeYDm2LMxrMUwK9/8j/8imHlyWzRSlXnckCm3ytqg1ssjj9xG9ySai8TssCHJnMSuii+RPec4uoAWJzrkbCINZ8TpB42Xgdzz6cI8NICOq6v2ToNnAVY9Uwtb/gMf5Jmh3MGO3xNPj8p6hS1vyGbsUEDPUHlVYJm28YoyTTABdxs7P7ml7kyJKHRNNULhUocRt5oDz8LrFIQIDAQAB";
        String txt = "+cXTyT5O/UPUc23WREB2j8Cp2EWkb+/IImsG72Qvnh9xnAZGGKjTNWnYREThHpgXJF2YWn+fj7nLPapA3WI36IH27T8XpMvUMesrlQgskgBZm2ot7IU9mpXqvh9LRiYbP89Pgs5kjEEoXwaNJLG5d0+ATdQ==";
        RSA rsa2 = new RSA(null,Base64Utils.decode(pbk_c.getBytes("UTF-8")));
        byte[] bytes = rsa2.decrypt(Base64Utils.decode(txt.getBytes("UTF-8")), KeyType.PublicKey);
        System.out.println(new String(bytes));
    }

    public static void encByPbkey() throws UnsupportedEncodingException {
        String pbk_c = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2onzcIS0MgvijpQJkhSya4s7Sog/fMxOoONASwf30hzOcD/XA3yFlCbyXevxkLYDdaDScjJH2ocIT/f40SpZH0PVTUjCsxdGtU+vWp/4zRVf/QjEghZ/5MMwK85LX7c7bIR272Nai0PFJj+FZhSK0G3ZuHi3GUV2lMiHSMXq75YG8m54tw0mk1wFa+rf91BXZi06uDfngKIoTkyJBXn6PSszrNnzP/iDUzpO9OUE7uDmvusm78VHEVxpN9BFZdv3vIfRJ34IbtBRuuriSSxkLHWvA6e93ph+01OLFwXQlL9cXcbuWZdwH1YBjVVyD3PpSH6+Lc0euKts2UXgaTSKOQIDAQAB";
        byte[] decode = Base64Utils.decode(pbk_c.getBytes("UTF-8"));

        RSA rsa = new RSA(null,decode);
        byte[] bytes = rsa.encrypt("asb阿萨德1111".getBytes("UTF-8"),KeyType.PublicKey);
        String s = new String(Base64Utils.encode(bytes));
        System.out.println(s);
    }

}
